package com.slwish.xty.mvp.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.slwish.common.mvp.presenter.BasePresenter;
import com.slwish.common.utils.LogUtils;
import com.slwish.xty.R;
import com.slwish.xty.config.UrlConfig;
import com.slwish.xty.entity.UserModel;
import com.slwish.xty.mvp.contract.CreditPointsContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.ProgressDialogCallBack;
import com.zhouyou.http.exception.ApiException;

/**
 * 模块说明：信用分查询
 *
 * @author 陈浩
 * @date 2017/12/17
 */

public class CreditPointPresenter extends BasePresenter<CreditPointsContract.View> implements CreditPointsContract.Presenter {
    public CreditPointPresenter(CreditPointsContract.View view) {
        super(view);
    }

    /**
     * 根据用户id查询信用分
     *
     * @param aid
     */
    @Override
    public void queryUserCredit(Context context, String aid) {
        EasyHttp.post(UrlConfig.QUERY_USER_CREDIT_URL)
                .params("aid", aid)
                .execute(new ProgressDialogCallBack<String>(showProgressDialog(context)) {
                    @Override
                    public void onSuccess(String s) {
                        try {
                            LogUtils.i("xx","json:"+s);
                            JSONObject jsonObject = JSON.parseObject(s);
                            String resultCode = (String) jsonObject.get("resultCode");
                            String resultMessage = (String) jsonObject.get("resultMessage");
                            String data = jsonObject.getString("data");
                            //return
                            if (!TextUtils.equals(resultCode, "0")) {
                                setError(resultMessage);
                                return;
                            }
                            UserModel model = JSON.parseObject(data,UserModel.class);
                            if (model!=null&&getView()!=null)
                                getView().queryUserCreditSuccess(model);
                        } catch (Exception e) {
                            setError(R.string.loadig_failed_text);
                        }
                    }

                    @Override
                    public void onError(ApiException e) {
                        super.onError(e);
                    }
                });
    }
}
